
var tableIns;
layui.use([ 'jquery', 'layer', 'form', 'table'  ], function() {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var laydate = layui.laydate;
    //渲染数据表格
    tableIns=table.render({
        elem: '#selRegister'   //渲染的目标对象
        ,url:'/hospitalization/selRegister' //数据接口
        ,title: '用户数据表'//数据导出来的标题
        ,toolbar:"#ToolBar"   //表格的工具条
        ,height:'full-200'
        ,page: 1  //是否启用分页, limits: [10, 20, 30]
        ,limits: [7, 20, 30]
        ,limit: 7//默认采用3
        ,cols: [[   //列表数据
            {field:'registerid', title:'住院号',align:'center',width:'7%',sort: true}
            ,{field:'userName', title:'姓名',align:'center',width:'6%'}
            ,{field:'sex', title:'性别',align:'center',width:'6%',sort: true}
            ,{field:'age', title:'年龄',align:'center',width:'6%',sort: true}
            ,{field:'address', title:'地址',align:'center',width:'10%'}
            ,{field:'phone', title:'电话',align:'center',width:'11%'}
            ,{field:'idcard', title:'身份证号',align:'center',width:'15%'}
            ,{field:'registerDate', title:'入院日期',align:'center',width:'13%',sort: true}
            ,{field:'department', title:'科室',align:'center',width:'6%'}
            ,{field:'doctorName', title:'医生',align:'center',width:'6%'}
            ,{field:'bedName', title:'床位',align:'center',width:'7%'}
            ,{fixed: '', title:'操作', toolbar: '#ToolBarss',align:'center',width:'7%'}
        ]]
    })
    //门诊信息显示
    tableInss=table.render({
        elem: '#show'   //渲染的目标对象
        ,url:'' //数据接口
        ,title: '门诊信息显示'//数据导出来的标题
        ,height:'full-200'
        ,page: 1  //是否启用分页, limits: [10, 20, 30]
        ,limits: [7, 20, 30]
        ,limit: 7//默认采用3
        ,cols: [[   //列表数据
            {field:'userName', title:'姓名',align:'center',width:'12%'}
            ,{field:'sex', title:'性别',align:'center',width:'10%',sort: true}
            ,{field:'age', title:'年龄',align:'center',width:'10%',sort: true}
            ,{field:'phone', title:'电话',align:'center',width:'16%'}
            ,{field:'idcard', title:'身份证号',align:'center',width:'24%'}
            ,{field:'diagnose', title:'转院原因',align:'center',width:'17%'}
            ,{fixed: '', title:'操作', toolbar: '#ToolBars',align:'center',width:'11%'}
        ]]
    })
    //模糊查询
    $("#doSearch").click(function(){
        var params=$("#searchFrm").serialize();
        tableIns.reload({
            url:"/hospitalization/selRegister?"+params
        })
    });

    //监听头部工具栏事件
    table.on("toolbar(selRegister)",function(obj){
        switch(obj.event){
            case 'add':
                //清空表单数据
                $("#dataFrm")[0].reset();
                $("#doctorId").empty();
                $("#bedId").empty();
                $("#doctorId1").empty();
                $("#bedId1").empty();
                openAddDoctor();
                break;
        };
    })

    var url;
    var mainIndex;
    //打开添加页面
    function openAddDoctor(){
        mainIndex=layer.open({
            type:1,
            title:'入院登记',
            content:$("#saveOrUpdateDiv"),
            area:['800px','550px'],
            success:function(index){
                url="/hospitalization/addRegister";
            }
        });
    }
    //初始化添加or修改到下拉框中的值
    $.ajax({
        url: '/hospitalization/selDepartment',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            $.each(data, function (index, item) {
                $('#departmentId').append(new Option(item.department, item.departmentId));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }
    })
    //查询医保折扣
    $.ajax({
        url: '/hospitalization/selDis',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            $.each(data, function (index, item) {
                $('#discount').append(new Option(item.moneytype, item.percent));// 下拉菜单里添加元素
            });
            layui.form.render("select");//重新渲染 固定写法
        }
    })
    // select下拉框选中触发事件
    form.on("select(department)", function(data){
        $("#doctorId").empty();
        $("#bedId").empty();
        $("#doctorId1").empty();
        $("#bedId1").empty();
        $.ajax({
            url: '/hospitalization/selDoctor',
            dataType: 'json',
            type: 'post',
            data:{
                departmentId:data.value
            },
            success: function (data) {
                $.each(data, function (index, item) {
                    $('#doctorId').append(new Option(item.doctorName, item.doctorId));// 下拉菜单里添加元素
                    $('#doctorId1').append(new Option(item.doctorName, item.doctorId));
                });
                layui.form.render("select");//重新渲染 固定写法
            }
        })
        //查询床位
        $.ajax({
            url: '/hospitalization/selBed',
            dataType: 'json',
            type: 'post',
            data:{
                departmentId:data.value
            },
            success: function (data) {
                $.each(data, function (index, item) {
                    $('#bedId').append(new Option(item.bedname, item.bedId));// 下拉菜单里添加元素
                    $('#bedId1').append(new Option(item.bedname, item.bedId));
                });
                layui.form.render("select");//重新渲染 固定写法
            }
        })
    });
    //保存

    var isclick= true;
    function click() {
        if (isclick) {
            isclick = false;
            if ($("input[name='age']").val() <= 0) {
                layer.msg("请输入正确年龄!");
                $(this).select();
            }
            //序列化表单数据
            var params = $("#dataFrm").serialize();
            $.post(url, params, function (obj) {
                layer.msg(obj);
                //关闭弹出层
                layer.close(mainIndex)
                //刷新数据 表格
                tableIns.reload();
                //定时器
                setTimeout(function () {
                    isclick = true;
                }, 1000);
            })
        }
    }
    form.on("submit(doSubmit)",function(obj){
        click();
    });
    //转院
    var mainIndexs;
    $("#zhuan").click(function(){
        mainIndexs=layer.open({
            type:1,
            title:'门诊人员',
            content:$("#yuan"),
            area:['800px','550px'],
            success:function(index){
                tableInss.reload({
                    url:"/hospitalization/selDoor"
                })
            }
        });
    });
    //监听门诊的行事件
    table.on('tool(show)', function(obj){
        if(obj.event=='sel'){
            $.each(obj.data, function (index, item) {
                $("#"+index).val(item);
                if(index=="sex"){
                    $("input[name='sex']").each(function(){
                        if($(this).val()==item){
                            $(this).prop("checked",true);
                            form.render(); //更新单选框
                        }
                    })
                }
            });
            layer.close(mainIndexs)
        }
    })
    //监听患者表的行事件
    table.on('tool(selRegister)', function(obj){
        if(obj.event=='upd'){
            $("#frm")[0].reset();
            $("#doctorId").empty();
            $("#bedId").empty();
            $("#doctorId1").empty();
            $("#bedId1").empty();
            $("#t1").empty();
            $("#t2").empty();
            $("#t3").empty();
            $("#t4").empty();
            $("#t1").append("<option>"+obj.data.department+"</option>");
            $("#t2").append("<option>"+obj.data.doctorName+"</option>");
            $("#t3").append("<option>"+obj.data.bedName+"</option>");
            $("#t4").val(obj.data.registerid);
            mainIndexss=layer.open({
                type:1,
                title:'转院',
                area:['700px','500px'],
                content:$("#ke"),
                success:function(index){
                    //在科室下拉框添加内容
                    $.ajax({
                        url: '/hospitalization/selDepartment',
                        dataType: 'json',
                        type: 'post',
                        success: function (data) {
                            $.each(data, function (index, item) {
                                $('#departmentId1').append(new Option(item.department, item.departmentId));// 下拉菜单里添加元素
                            });
                            layui.form.render("select");//重新渲染 固定写法
                        }
                    })
                }
            });
        }
    })
    form.on("submit(sub)",function(obj){
        //序列化表单数据
        var params=$("#frm").serialize();
        $.post("/hospitalization/updKe",params,function(obj){
            layer.msg(obj);
            //关闭弹出层
            layer.close(mainIndexss)
            //刷新数据 表格
            tableIns.reload();
        })
    });
});
